<template>
  <div>
    <el-drawer v-model="isOpen" size="40%" :before-close="onBeforeClose" direction="rtl">
      <template #header>
        <h4>{{ title }}</h4>
      </template>
      <div style="width: 90%;">
        <el-form ref="userFormRef" :model="userFormModel" label-width="100px">
          <el-form-item label="账号" prop="username">
            <el-input v-model="userFormModel.username" placeholder="请输入用户名" autocomplete="auto" />
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="userFormModel.email" placeholder="请输入邮箱" />
          </el-form-item>
          <el-form-item label="头像" prop="avatar">
            <el-input v-model="userFormModel.avatar" placeholder="请输入头像" />
          </el-form-item>
          <el-form-item label="昵称" prop="nickname">
            <el-input v-model="userFormModel.nickname" placeholder="请输入昵称" />
          </el-form-item>
          <el-form-item label="部门" prop="dept_id">
            <el-select v-model="userFormModel.dept_id" placeholder="请选择部门">
              <el-option v-for="department in departments" :key="department.id" :label="department.name"
                :value="department.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="角色" prop="role_id">
            <el-select v-model="userFormModel.role_id" placeholder="请选择角色">
              <el-option v-for="role in roles" :key="role.id" :label="role.name" :value="role.id" />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div style="flex: auto">
          <el-button @click="onCancel">取消</el-button>
          <el-button type="primary" :loading="isLoading" @click="onSubmit">
            {{ isLoading ? '提交中 ...' : '提交' }}
          </el-button>
        </div>
      </template>
    </el-drawer>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, nextTick } from 'vue';
import useCommonStore from "@/store/common"
import useSystemStore from "@/store/system"
import { storeToRefs } from "pinia"
import type { FormInstance } from 'element-plus';

var commonStore = useCommonStore()
var { roles, departments } = storeToRefs(commonStore)
var systemStore = useSystemStore()
var userFormRef = ref<FormInstance>()

var title = "新增用户"
var isOpen = ref(false)
var isLoading = ref(false)
var isEdit = ref(false)

var userFormModel = reactive({
  id: 0,
  username: "",
  email: "mmm",
  avatar: "",
  nickname: "",
  dept_id: "",
  role_id: ""
})

// 关闭前
function onBeforeClose(done: any) {
  if (isLoading.value) {
    return
  }
  ElMessageBox.confirm('你的表单数据还没有提交，确定关闭吗？', '系统提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
      center: true,
    })
    .then(() => {
      done()
    })
    .catch(() => {
      // catch error
    })
}
// 取消
function onCancel() {
  isLoading.value = false
  isOpen.value = false
}
// 提交
function onSubmit() {
  isLoading.value = true
  setTimeout(async () => {
    try {
      if (isEdit.value) {
        await systemStore.updateUserAction(userFormModel.id, userFormModel)
      } else {
        await systemStore.addUserAction(userFormModel)
      }
      isLoading.value = false
      isOpen.value = false
    } catch (error) {
      isLoading.value = false
    }
  }, 400)

}
function open(row?: UserModel) {
  if (row) {
    for (const key in userFormModel) {
      userFormModel[key] = row[key]
    }
    isEdit.value = true
  } else {
    isEdit.value = false
    for (const key in userFormModel) {
      userFormModel[key] = ""
    }
  }
  isOpen.value = true
}

defineExpose({ open })
</script>

<style lang="scss" scoped>
.main {
  color: red;
}
</style>
